package timing.ukulele.gateway.http;

import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import timing.ukulele.gateway.http.api.PortalApi;

/**
 * Http Interface注入ioc配置
 */
@Configuration
public class ExchangeBeanConfig {
    private final WebClient.Builder webClient;
    private ReactorLoadBalancerExchangeFilterFunction filter;


    public ExchangeBeanConfig(
            WebClient.Builder webClient,
            ReactorLoadBalancerExchangeFilterFunction filter) {
        this.webClient = webClient;
        this.filter = filter;
    }

    /**
     * 注入PortalApi
     *
     * @return PortalApi
     */
    @Bean
    public PortalApi portalExchange() {
        WebClient client = this.webClient.filter(filter).baseUrl("lb://ukulele-portal/").build();
        HttpServiceProxyFactory httpServiceProxyFactory = HttpServiceProxyFactory
                .builderFor(WebClientAdapter.create(client))
                .build();
        return httpServiceProxyFactory.createClient(PortalApi.class);
    }

}